<?php
require './vendor/autoload.php';

$worker = new \Workerman\Worker('tcp://0.0.0.0:8888');
$worker->onMessage = function (\Workerman\Connection\TcpConnection $connection,$data){
    if (chr(0x00).chr(0x05)."swing" === $data){
        var_dump("逗猫棒动了一下");
        diandeng();
    }
};
$worker->onConnect = function (\Workerman\Connection\TcpConnection $connection){
    var_dump("有客户端连接".$connection->getRemoteAddress());
    $connection->send(chr(0x00).chr(0x04)."ping");

    $connection->timer_id = \Workerman\Timer::add(15,function () use ($connection){
        $connection->send(chr(0x00).chr(0x03)."vib");
        echo $connection->getRemoteAddress()."发送了数据".PHP_EOL;
    });
};

$worker->onError = function(TcpConnection $connection, $code, $msg)
{
    echo "error $code $msg\n";
};

$worker->onClose = function (\Workerman\Connection\TcpConnection $connection){
    \Workerman\Timer::del($connection->timer_id);
    echo $connection->getRemoteAddress().'断开了连接'.PHP_EOL;
};
//$worker->onWorkerStart = function (\Workerman\Worker $worker){
//    $mgr = new \Illuminate\Database\Capsule\Manager();
//    $mgr->addConnection([
//        'driver' => 'mysql',
//        'host' => 'localhost',
//        'database' => 'emer',
//        'username' => 'root',
//        'password' => 'root',
//        'charset' => 'utf8',
//        'collation' => 'utf8_unicode_ci',
//        'prefix' => '',
//    ]);
//    $mgr->bootEloquent();
//    $mgr->setAsGlobal();
//};

\Workerman\Worker::runAll();


function diandeng(){
    $gpio = new \PiPHP\GPIO\GPIO();
    $pin4 = $gpio->getOutputPin(4);
    $pin4->setValue(\PiPHP\GPIO\Pin\PinInterface::VALUE_HIGH);
    usleep(300*1000);
    $pin4->setValue(\PiPHP\GPIO\Pin\PinInterface::VALUE_LOW);
}
